Network - Netcode
Source suggestions
-
https://www.youtube.com/@NetworkNext
-
https://www.gafferongames.com/
About
-
"A set of specific techniques and algorithms that make network communication work in a multiplayer game. This includes how data packets are sent and received, how network latency is handled, and how the game deals with packet loss, interpolation, extrapolation, etc."
Synchronization
Clock Sync
Full Synchronization with Input Lag
Lockstep
-
"The inputs of both players are exchanged with each other, so both players will be running the same simulation."
-
A stricter synchronization method where all machines involved in the system must process the same inputs at the same time before advancing.
-
Requirements :
-
The game must be absolutely deterministic .
-
-
Advantages :
-
Keeps all players perfectly synchronized.
-
Prevents cheating that could occur in client-authoritative netcodes.
-
Uses low bandwidth, as only inputs are sent.
-
-
Disadvantages :
-
Can be affected by input lag , since all players must wait for each other's inputs before advancing.
-
If one player has high latency, all others are impacted (the classic "slowdown" problem in old RTS games).
-
Harder to scale for games with many simultaneous players.
-
-
Uses :
-
RTS (Real-Time Strategy):
-
StarCraft, Age of Empires
-
-
TBS (Turn-Based Strategy):
-
XCOM Multiplayer, Civilization
-
-
Physics-Based Deterministic Games:
-
Games that require identical simulations on all machines.
-
-
Lag Compensation
Interpolation
-
Useful for other players .
-
Uses past states to create smooth transitions between positions.
-
Renders the world 50~100ms in the past , to know where the player is and where they will be, interpolating the position between these two points, considering the distance traveled and the time spent.
-
Advantages :
-
Smooth movements without sudden jumps.
-
-
Disadvantages :
-
Introduces input lag indirectly, by rendering the world in the past.
-
-
Use :
-
MOBAs and games where a small delay does not affect gameplay.
-
-
Impressions :
-
Games relying on mouse clicks can work, but for more tactile inputs like keyboard movement, input lag becomes very noticeable.
-
-
-
The person uses a 20Hz server.
-
-
Strategies for Dealing with Rubberbanding
-
Smooth and Gradual Corrections :
-
Instead of instantly moving the player to the corrected position, the server sends the correction gradually, over a few milliseconds (LERP, Linear Interpolation).
-
If the server detects that the player’s position was incorrect due to prediction, it moves the player smoothly to the correct position over 100-200ms. Transition time depends on the prediction error magnitude.
-
-
Speed Adjustment :
-
When the player’s position is incorrect, adjust their speed or movement to correct the discrepancy instead of repositioning them immediately.
-
If a player predicted a wrong movement, instead of "jumping" to the correct position, they can move slowly until they reach the correct spot.
-
-
Correction Size Limitation :
-
The server can set limits for allowed corrections. If the difference between predicted and correct positions is too large, the correction is done more cautiously, or temporarily ignored until the player "approaches" the correct point.
-
If the prediction error is 10 meters, the server might allow only a 3-meter correction at a time instead of teleporting the player all at once.
-
-
"Less Frequent State Correction" System :
-
The server can correct the player’s state less frequently or only when the error is significant. This can include a strategy to correct the state periodically (e.g., every 200ms or 500ms) without trying to correct all prediction errors at once.
-
The server checks and corrects the player’s position only every second, or every 200ms, depending on the perceived error.
-
Prediction
-
My interpretation :
-
For the player:
-
"The client goes ahead by processing the input locally."
-
-
For other players:
-
"The client assumes that the other player will maintain the same direction and/or input."
-
-
-
Prediction of local input results before server confirmation.
-
When to use :
-
A way to reduce input lag.
-
When a player performs an action, and the game needs to display it immediately without waiting for the server.
-
Example:
-
You press a key to move → the character moves immediately on your PC without waiting for the server.
-
-
The client displays slightly past states to smooth the movement of other players.
-
-
Advantages :
-
More fluid and responsive gameplay.
-
-
Disadvantages :
-
If the server disagrees, it can cause sudden corrections (rubberbanding).
-
-
Use :
-
FPS, MOBAs, any game requiring fast response.
-
Extrapolation
-
Prediction of other players/objects positions when the server does not send new packets.
-
"Sometimes during interpolation, the future frame is not stored due to high lag or packet loss, etc. In this case, Interpolation no longer works."
-
When to use :
-
A form of lag compensation.
-
When there is packet loss or high latency, and the game needs to "guess" where a player is.
-
Example:
-
The game doesn’t receive the next packet about an enemy’s position → assumes they continue in the same direction until new information arrives.
-
-
If packets are delayed, the game estimates the future position of other players.
-
-
Advantages :
-
Prevents freezing or delayed movement display.
-
-
Disadvantages :
-
If a player does something unexpected, it can cause jumps or sudden corrections.
-
-
Use :
-
Racing games, sports games, where movement is predictable.
-
Correcting Wrong States (or Predictions)
-
Techniques used together with Prediction.
Replication
-
Replicates data and events occurring on the Host / Server.
-
This strategy makes more sense for P2P architectures, since a dedicated server does not necessarily have information to "replicate."
-
I understand Replication and Reconciliation as similar in philosophy, but Reconciliation uses a Prediction model, while Replication uses replication of host P2P states and events to later resynchronize.
-
P2P and Netcode in Halo Reach .
-
{26:39 -> 35:00}
-
Uses a latency diagram to illustrate 'grenade throwing'.
-
{32:17} Solution.
-
"Never have input lag; if the player presses a button, they get frustrated if no immediate action happens on screen."
-
-
-
{35:00 -> 44:00}
-
Uses a latency diagram to illustrate 'Armor Lock'.
-
{41:20} Solution.
-
To fix, "lag debt" was paid on the host, so the host activated the shield earlier than it should, giving the requesting client feedback that things are happening in real-time.
-
This is interesting because it modifies mechanics on the host to cancel the perception of latency.
-
Instead of the client lagging, the host was faster.
-
-
-
-
-
{44:00 -> 57:10}
-
Uses a latency diagram to illustrate 'opponent finishing move'.
-
To fix, an interpolation of the attacker’s position during the start of the finishing animation was done, positioning both attacker and target better to play the animation correctly.
-
"This is not noticeable for the attacker, since the animation start moves the camera so quickly."
-
-
-
{Rest of video}
-
Interesting discussion on profiling, recording demos, and tools to gather data before optimizing.
-
-
A 'network prioritization' system is made for each object and entity on screen, dynamically varying depending on how perceptible and relevant the object is, optimizing bandwidth usage and not spending resources on imperceptible things.
-
Reconciliation
-
How errors are handled :
-
Smoothly adjusts the player’s state.
-
-
Disadvantages :
-
Can cause rubberbanding and small teleports.
-
-
Uses :
-
World of Warcraft.
-
In World of Warcraft , pressing "W" makes your character start moving immediately (Prediction), and the server confirms later. Other players see your interpolated position to avoid visual teleporting.
-
-
-
Netfox .
-
'Lag compensation with Client-side Prediction and Server-side Reconciliation'.
-
Docs .
-
-
Rollback
-
Rollback vs Reconciliation :
-
Reconciliation:
-
"Rewinds" the local game state to the last server-confirmed state and replays buffered inputs to stay in sync.
-
Primarily used for your own client's actions (e.g., moving your character).
-
Doesn’t usually handle interactions with other players/world objects beyond basic corrections.
-
In an FPS, your client shows you moving forward instantly, but if the server says you were blocked by a wall, you’re "snapped back" to the correct position.
-
-
Rollback:
-
A more generalized technique where all players' inputs are predicted and rolled back if mispredicted.
-
Requires deterministic logic and often peer-to-peer input exchange.
-
In a fighting game, if Player A’s punch was supposed to hit at frame 42 but Player B’s block input arrived late, the game rolls back to frame 42 and re-simulates with the correct inputs.
-
-
Both techniques involve rewinding and replaying to fix mispredictions.
-
Client-side reconciliation is a limited form of rollback—it only handles the local player's state.
-
Full rollback systems (like GGPO) apply the concept globally to all entities.
-
-
Also known as Deterministic Lockstep with Rollback .
-
Deals more with Inputs than events, like Lockstep, requiring a deterministic game.
-
Use without Prediction :
-
The game waits to receive inputs from all players before processing the next frame.
-
If an input arrives late, the game needs to "rewind" (rollback), recalculate affected frames, and update the state.
-
-
Use with Prediction :
-
While waiting for the opponent’s real inputs, the client predicts what will probably happen and displays it on screen.
-
If the prediction is wrong, rollback occurs to correct the state.
-
"Goes back in time and re-applies inputs in case of errors."
-
-
How errors are handled :
-
Rewinds the game and reapplies inputs.
-
-
Requirements :
-
.
-
-
Uses :
-
.
-
-
Advantages :
-
.
-
-
Disadvantages :
-
Can cause visual glitches if too much rollback occurs.
-
-
Rollback Netcode in Godot {Playlist with 11 videos} .
-
-
Fork of the addon above.
-
-
The videos are very well produced.
-
Uses Godot 3, unfortunately.
-
The game made is a peer-to-peer, Client-Authority game.
-
The system is based on completely restructuring the existing system, using several special virtual functions and a state and input storage system.
-
Video 1 already explains the basic setup.
-
I watched only videos 1 and 2.
-
Using Rollback instead of Lockstep .
-
Watched up to 25:00.
-
A super technical and interesting video.
-
The Rollback system was implemented to synchronize between peers, so that frames lost due to latency are quickly simulated in the same frame to ensure smoothness.
-
Uses a P2P connection, where only input information is sent between the Client-server (peer host) and the Client (another peer).
-